package cn.kgc.dao;

import cn.kgc.pojo.Category;
import cn.kgc.util.DatabaseUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CategoryDaoImpl extends  BaseDao implements  CategoryDao {



    @Override
    public Category selectCategoryById(Connection connection , Integer id) {
        String sql ="select id ,name, createDate from news_category where id=?";
        Category category =null;
        ResultSet resultSet=null;
        try {
            resultSet = execuseQuery(connection,sql,id);
            while(resultSet.next()){
                Integer rid= resultSet.getInt(1);
                String name=resultSet.getString(2);
                Date date =resultSet.getDate(3);
                category  =new Category(rid,name,date);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DatabaseUtil.closeResult(null,null,resultSet);
        }
        return category;
    }

    @Override
    public List<Category> selectCategory(Connection connection) throws SQLException {
        String sql ="select id,name,createDate from news_category";
        ResultSet rs =execuseQuery(connection,sql);
        List<Category> list =new ArrayList<>();
        while(rs.next()){
            int id=     rs.getInt(1);
            String name=rs.getString(2);
            Date date=rs.getDate("createDate");
            Category category =new Category(id,name,date);
            list.add(category);
        }

        return list;
    }
}
